'''
@Date:2022/10/2  14:22
@Author:一加一
'''

import pytest
from tools.common import *
from tools.get_headerjson import GetHeaderjson,GetcJson
from conf.api_path import ApiPath
from tools.get_userjson import GetuserJson
from mysqltest.mysql_db import sqlconnect
#from tools.get_wjson import *
import json

import sqlite3
import time
'''处理登录：统一登录S、业务系统登录W'''

# 实例化对象
api_path = ApiPath()
get_header = GetHeaderjson(None)
get_userinfo = GetuserJson(None)
get_cjson = GetcJson(None )
#get_wjson = GetwJson(None)


@pytest.fixture(scope="session",autouse=True)
def s_login():
    # s登录：用户名密码登录
    url = api_path.login #调用api_path方法获取url
    headers = get_header.get_s_login_headers() #调用get_header获取s系统接口请求头
    data = json.dumps(get_cjson.get_login()) #调用get_userinfo获取入参
    res_json = Common.r_post_form(url=url, headers=headers, data=data)
    print("打印url:",type(url),url)
    print("打印data:",type(data),data)
    print("打印headers:",type(headers),headers)
    # 获取code
    code = res_json['data']['token']
    print("打印code:",code)

    # 生成stoken
    #res_json = Common.r_post_form(url=api_path.s_exchangeToken_url,headers=get_header.get_s_login_headers(),data=code)
    # 将生成的sToken写入headers.json文件
    #get_header.set_s_headers(res_json['data']['sToken'])
    get_header.set_s_headers(code)
    print("校验token是否写入：",get_header.get_s_headers())






# 用例级别的前置后置条件
@pytest.fixture()
def case_fixture():
    sql = "select play_status from device where is_del = 0 and device_id = 10032  and dept_id = 1"
    sql_result = sqlconnect.sqljudje(sql)[0][0]

    try:
        while True:
            # 执行查询
            sqlconnect(sql)

            for row in results:
                print(row)  # 处理查询结果

            # 休眠指定时间
            time.sleep(3)
        while True:
            if sql_result == 0:
                break
            else:
                tsql_result = sqlconnect.sqljudje(sql)[0][0]
        yield
        # 后置条件
        print("前置条件已符合，可以执行下一用例")

    except KeyboardInterrupt:
        print("程序被用户中断")

    # 前置条件




    sql = "select play_status from device where is_del = 0 and device_id = 10032  and dept_id = 1"
    try:
        while True:
            # 执行查询
            sqlconnect(sql)

            for row in results:
                print(row)  # 处理查询结果

            # 休眠指定时间
            time.sleep(3)
    except KeyboardInterrupt:
        print("程序被用户中断")

    # 关闭数据库连接
    connection.close()


"""
@pytest.fixture(scope="session",autouse=True)
def test_w_login():
    # s登录：用户名密码登录
    url = api_path.s_login_url #调用api_path方法获取url
    headers = get_header.get_s_login_headers() #调用get_header获取s请求头
    data = get_userinfo.get_w_login_info() #调用get_userinfo获取入参
    res_json = Common.r_post_form(url=url, headers=headers, data=data)
    # 获取code
    code = res_json['data'].split('?')[-1]

    # 生成stoken
    res_json = Common.r_post_form(url=api_path.s_exchangeToken_url,headers=get_header.get_s_login_headers(),data=code)
    # 将生成的sToken写入headers.json文件
    get_header.set_w_headers(res_json['data']['sToken'])
    get_header.set_s_headers(res_json['data']['sToken'])

    # 查询s系统员工列表获取T-id写入headers.json文件的T-Id
    res_json = Common.r_s_post(url=api_path.employee_paging_list,
                             headers=get_header.get_s_headers(),
                             json=get_wjson.get_w_companyId())
    # 将响应结果转换成json格式
    rep_json = res_json.json()
    # 断言
    Common.assert_s_code_message(rep_json)
    # 将生成的companyId写入headers.json文件（w_headers对应的T-Id）
    TenantId = str(rep_json['data']['pageData'][0]['companyId'])
    get_header.set_w_headers_companyId(TenantId)

"""